home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / me_cd25.zip / MC2MUTT.ZIP / SORT.MUT < prev    next >
Lisp/Scheme  |  1992-11-09  |  1KB  |  51 lines

  1. (defun 
  2.   sort (array int list-to-sort 1)(int n)    ; shell sort an array of n ints
  3.   {
  4.     (int gap i j t k)
  5.  
  6.     (gap (/ n 2))
  7.     (while (> gap 0)
  8.     {
  9.       (i gap)
  10.       (while (< i n)
  11.       {
  12.     (j (- i gap))
  13.     (while (and (>= j 0) (> (list-to-sort j)(list-to-sort (+ j gap))))
  14.     {
  15.       (k (+ j gap))(t (list-to-sort j))
  16.       (list-to-sort j (list-to-sort k))(list-to-sort k t)
  17.       (-= j gap)
  18.     })
  19.         (+= i 1)
  20.       })
  21.       (/= gap 2)
  22.     })
  23.   }
  24. )
  25.  
  26.  
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28. ;;;;;;;;;;;;;;;;;  test ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30.  
  31. (const NUMBER    0x03)
  32.  
  33. (include random.mut)
  34.  
  35. (array int list-to-sort 501) (int n j)
  36.  
  37. (defun MAIN
  38. {
  39.   (n (convert-to NUMBER (ask "n = ")))
  40.  
  41.   ;(srand 123)
  42.   (for (j 0) (< j n)(+= j 1) (list-to-sort j (rand)))
  43.  
  44.   (for (j 0)(< j n)(+= j 1) (msg "Random list[" j "] = " (list-to-sort j)))
  45.  
  46.   (sort list-to-sort n)
  47.  
  48.   (msg "--------------------------")
  49.   (for (j 0)(< j n)(+= j 1) (msg "Sorted list[" j "] = " (list-to-sort j)))
  50. })
  51.